﻿<div id="AddForm">

    <table>
        <tr>
            <th>Title</th>
            <td>
                <input type="text" class="required" id="Title" placeholder="Name for run" maxlength="500"/>
            </td>
        </tr>
        <tr>
            <th>DataSet</th>
            <td>
                <select id="DataSetSelect"></select>
            </td>
        </tr>
        <tr>
            <th>Fitting Range</th>
            <td>
                <input id="Distance" class="required" type="number" placeholder="Distance" style="width: 5em;" />
                <select id="Is3D">
                    <option value="0">Angular (2D)</option>
                    <option value="1">Linear (3D Projection)</option>
                </select>
            </td>
        </tr>
        <tr>
            <th>V Bins (3D)</th>
            <td>
                <input type="number" id="vmin" /> - <input type="number" id="vmax" />
            </td>
        </tr>
        </table>

               <!-- <div class="ui-state-highlight ui-corner-all mb">               
                    <div>Morphology filters are based on the data catalog's format. </div>
                    <div>Filters rely on SQL Server's LIKE operator</div>
              
                    <p>
                        <b>%</b>
                        Any string of zero or more characters.
                    </p>
                    <p>
                        <b>_ (underscore)</b>
                        Any single character.
                    </p>
                    <p>
                        <b>[ ]</b>
                        Any single character within the specified range ([a-f]) or set ([abcdef]).
                    </p>
                    <p>
                        <b>[^]</b>
                        Any single character not within the specified range ([^a-f]) or set ([^abcdef]).
                    </p>
             </div>
                   -->
               
             
    
        <p>
            <b>Primary Morph.</b>
            
               <!--- <input type="text" id="SourceFilter" placeholder="Inclusive filter" />
                <span class="button" data-icon="fa-check" onclick="PreviewResults('#SourceFilter');">Test</span>
                   -->
            <select id="SourceFilter"></select>
            
        </p>

        <p>
         <b>Secondary Morph. </b>
            
               <!--- <input type="text" id="NeighborFilter" placeholder="Inclusive filter" />
                <span class="button" data-icon="fa-check" onclick="PreviewResults('#NeighborFilter');">Test</span>
                   -->
            <select id="NeighborFilter"></select> 
        </p>
       <!--
     <p>
            <b>Notifications</b>
            
                
                    <input type="checkbox" checked  id="Pushover"/> <label for="Pushover">Pushover</label>
                
                
                    <input type="checkbox" checked id="Email"/> <label for="Email">Email</label>
                
            
        </p>-->

        <p>
           
                <span class="button"  data-icon="fa-save" onclick="TrySave();" id="QueueButton">Queue</span>
         
        </p>
  


</div>
<!---
<div style="width:700px;" id="PreviewContainer">
    <h4>Preview <span id="PreviewMatchesCount"></span></h4>
    <div id="previewresults"></div>
</div>
    -->

<script type="text/javascript">


    ///<reference path="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js" />
    ///<reference path="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" />
    ///<reference path="js/lib.js" />
    ///<reference path="js/md5.js" />
    
    $(document).ready(function () {


        $("#previewresults").jsontable();

        // load data sets for select.
        GetJsonSproc("usp_GetDataSets",
            
            function (json) {
                
                var sel = $("#DataSetSelect");
                sel.change(UpdateMorphFilterDropdowns);
                sel.empty();

                $.each(json, function(key, value)
                {
                    console.dir(value);
                    var o = $("<option>").text(value.Name).val(value.ID);
                    sel.append(o);
                });

                UpdateMorphFilterDropdowns();
            });


    });

    function UpdateMorphFilterDropdowns()
    {
        var dataSetId = $("#DataSetSelect > option:selected").val();

        GetJsonSproc("usp_GetMorphologyFilters",
            {
                DataSetId: dataSetId
            },

         function (json) {

             var sel = $("#SourceFilter, #NeighborFilter");
          
             sel.empty();

             var o = $("<option>").text("No Filter").val("");
             sel.append(o);

             $.each(json, function (key, value)
             {
                 console.dir(value);
                 var o = $("<option>").text(value.Name).val(value.RegexFilter);
                 sel.append(o);
             });


           
         });
    }

    function TrySave()
    {
        var form = $("#AddForm");
        var list = form.find("input.required").removeClass("ui-state-error");
        list.each( function ()
        {
            
            var t = $(this);
            console.dir(t);
            if(t.val() == "")
                t.addClass("ui-state-error");
        });

        if(form.find("input.required.ui-state-error").length == 0)
        {
            var parms = {
                UserId : localStorage.UserId,
                DataSetId: $("#DataSetSelect > option:selected").val(),
                Title: encodeURIComponent($("#Title").val()),
                Distance: $("#Distance").val(),
                SourceFilter: encodeURIComponent($("#SourceFilter").val()),
                NeighborFilter: encodeURIComponent($("#NeighborFilter").val()),
                Email : 0, //$("#Email").is(":checked") ? 1 : 0,
                Pushover: 0,// $("#Pushover").is(":checked") ? 1 : 0,
                Is3D: $("#Is3D > option:selected").val(),
                vmin: $("#vmin").val(),
                vmax: $("#vmax").val()
            }

         //   if (parms.SourceFilter == "") delete parms.SourceFilter;
            //  if (parms.NeighborFilter == "") delete parms.NeighborFilter;

            if (parms.Is3d == 0)
            {
                delete parms.vmin;
                delete parms.vmax;
            }
            else
            {
                if(parms.vmin == "" || parms.vmax == "")
                {
                    delete parms.vmin;
                    delete parms.vmax;
                }

            }

            $("#QueueButton").addClass("ui-state-disabled");
           
            GetJsonSproc("usp_AddDataRunRequest",
                parms,
                function (json) {
              
                    Growl("Request queued.");
                    $("#QueueButton").removeClass("ui-state-disabled");
                });
        }
    }


    function PreviewResults(id)
    {
        var regexString = $(id).val();
        if (regexString == "") regexString = null;

        var dataSetId = $("#DataSetSelect > option:selected").val();
        //console.log("data set id: " + dataSetId)

        var params = {
            DataSetId: dataSetId
        }
        if (regexString != null)
            params.filter = regexString;

        GetJsonMultiResultSproc("usp_TestFilter",
           params,
            function (json) {
                // console.dir(json);
                $("#PreviewMatchesCount").text(" (" + json.Table1[0].Matches + " matches)");
                $("#previewresults").jsontable( "option",
                    {
                        source: json.Table
                    });
            });

    }



</script>